package iwpl.project.servlet;

import iwpl.project.dao.ApartmentsDAO;
import iwpl.project.dao.AreasDAO;
import iwpl.project.entities.Apartments;
import iwpl.project.entities.Areas;
import iwpl.project.entities.Equipments;
import iwpl.project.entitymanagerhelper.EntityManagerHelper;


import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.List;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Control_Apartment extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Control_Apartment() {
		super();
	}

	public static void ExecuteControl(String Control, Apartments apart)
	{
		//TODO add code here
		ApartmentsDAO dao = new ApartmentsDAO();
		
		if(Control == "Save")
		{
			EntityManagerHelper.beginTransaction();
			dao.save(apart);
			EntityManagerHelper.commit();
		}
		
		if(Control == "Update")
		{
			EntityManagerHelper.beginTransaction();
			dao.update(apart);
			EntityManagerHelper.commit();
		}
		
		if(Control == "Delete")
		{
			EntityManagerHelper.beginTransaction();
			dao.delete(apart);
			EntityManagerHelper.commit();
		}
	}
	
	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
		
		doProcess(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		/*response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
		
		doProcess(request, response);
	}
	

	/**
	 * 
	 * This method is called by a doPost or doGet methods
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public String getSearch(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String sql="";
		String cboSearchAreaID=request.getParameter("cboSearchAreaID");
		String cboAprtmentNumber=request.getParameter("cboAprtmentNumber");
		String cboFloorNumber=request.getParameter("cboFloorNumber");
		String cboStatus=request.getParameter("cboStatus");
		
		if(cboSearchAreaID != "")
		{
			sql = "where AreaID = "+cboSearchAreaID+" AND ";
		}
		else
		{
			sql = "where ";
		}
		if(cboAprtmentNumber != "" )
		{
			sql= sql  + "ApartmentNumber = "+cboAprtmentNumber+" AND ";
		}
		
		
		if(cboFloorNumber != "" )
		{	
			sql = sql  + "FloorNumber = "+cboFloorNumber+" AND ";
		}
		
		if(cboStatus != ""  )
		{
			
			sql = sql  + "Status like '%"+cboStatus+"%'"+" AND ";
		}
		
		
			int count = sql.length();
			sql = sql.substring(0 , count - 5);
		
		
		return sql;
		
		
	}
	public void doProcess(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		Apartments apart = new Apartments();
		String sStatusDelete = request.getParameter("delete");
		String sStatusSearch = request.getParameter("btnSearch");
		if (sStatusSearch != null)
		{	
			ApartmentsDAO dao= new ApartmentsDAO();
			List<Apartments> list;
			list = dao.findWith(getSearch(request, response));
			
			HttpSession session = request.getSession(true);
			session.setAttribute("hehe", list);
			response.sendRedirect("../Manage_Apartment.jsp");
			
		}
		else
		{
		if(sStatusDelete != null)
		{	
			
			apart.setId(Integer.parseInt(sStatusDelete));
			ExecuteControl("Delete", apart);
			// After run command, return main page
			response.sendRedirect("/DepartmentManage/Manage_Apartment.jsp");
		}
		else
		{
		
		String sStatusInsert = request.getParameter("btnInsert");
		
		String sApartmentNumber = request.getParameter("txtApartmentNumber");
		Integer sFloorNumber = Integer.parseInt(request.getParameter("txtFloorNumber"));		
		String sStatus = request.getParameter("chkStatus");
		Double sValue = Double.valueOf(request.getParameter("txtValue"));
		
		
		
	
		String sStatusUpdate = request.getParameter("btnUpdate");
		
		String sID = request.getParameter("hiddenid");
		
		
		
		if(sStatus==null)
			apart.setStatus(false);
			
		else if(sStatus.toLowerCase().equals("on"))
			apart.setStatus(true);
		
		if(sStatusInsert != null)
		{
			
			if(request.getParameter("cboAreaID")!= null)
			{
			Integer sAreaID = Integer.parseInt(request.getParameter("cboAreaID"));
			AreasDAO dao = new AreasDAO();
			Areas area = dao.findById(sAreaID);
			
			
			apart.setAreas(area);
			apart.setApartmentNumber(sApartmentNumber);
			apart.setFloorNumber(sFloorNumber);
			apart.setValue(sValue);
		
			
			ExecuteControl("Save", apart);
			// After run command, return main page
			response.sendRedirect("/DepartmentManage/Manage_Apartment.jsp");
			}
		}
		
		
		
		if (sStatusUpdate != null)
		{	
			if(request.getParameter("cboAreaID")!= null)
			{
			Integer sAreaID = Integer.parseInt(request.getParameter("cboAreaID"));
			AreasDAO dao = new AreasDAO();
			Areas area = dao.findById(sAreaID);
			
			apart.setId(Integer.parseInt(sID));
			apart.setAreas(area);
			}
			apart.setApartmentNumber(sApartmentNumber);
			apart.setFloorNumber(sFloorNumber);
			apart.setValue(sValue);
			ExecuteControl("Update", apart);
			// After run command, return main page
			response.sendRedirect("/DepartmentManage/Manage_Apartment.jsp");
			
		}
		
		
		}}
		
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
